/*
 * @Author: 卢景滔
 * @Date: 2021-12-21 16:17:35
 * @LastEditTime: 2021-12-21 17:35:25
 * @LastEditors: 卢景滔
 * @Description: SharedWorker 共享线程代码
 */

// 计时器
let counter = 0

// 监听连接
self.addEventListener('connect', (e) => {
  const port = e.ports[0]
  port.onmessage = (res) => {
    console.log('共享线程接收到信息：', res.data)
    switch (res.data) {
      case 'counter++':
        counter++
        break
    }
    console.log('counter:', counter)
    port.postMessage(counter)
  }
})
